--- <action itemid="2403" script="RPG/knife.lua"/>
--- Knife maked by Kronos aka Hellboy ---
local transformTable = {
	[1947] = 2597 --- make letter
}

local skinable_corpse_table = {
--- [itemEx.id] = {item = {{id, chance, count, putInCorpse, voc}}, newitemEx_id}
	[2905] = {item = {{id = 11236, chance = 100}, {id = 2671, count = {1, 3}}}, putInCorpse = true}, -- sheep
	[4265] = {item = {{id = 5890, chance = 100}, {id = 2666, chance = 60, count = {1, 2}}}, putInCorpse = 0}, -- chicken, 4266 change to container

	-- Leather
	--- --- deer
	[3095] = {item = {{id = 5878}}, putInCorpse = false},
	--- --- wolf
	[3086] = {item = {{id = 5878, chance = 35}, {id = 2666, chance = 40, count = 2}}, putInCorpse = false},
	--- --- bear
	[3109] = {item = {{id = 5878, chance = 50, count = 2}}, putInCorpse = false}, 
	--- --- rotworm
	[3084] = {item = {{id = 5878, chance = 50, count = 2}}, putInCorpse = false},
	--- --- war wolf
	[2969] = {item = {{id = 5878, chance = 75}}, putInCorpse = false}, 
		
	-- Dragons leather
	[3104] = {item = {{id = 5876, chance = 80, count = 5}}, putInCorpse = false},
	[2844] = {item = {{id = 5876, chance = 80, count = 5}}, putInCorpse = false},
	[4256] = {item = {{id = 5876, chance = 80, count = 5}}, putInCorpse = false},
		
	-- Crocodile leather
	[4277] = {item = {{id = 5877, chance = 80}}, putInCorpse = false}, -- crocodile
		
	-- Red leather
	[4292] = {item = {{id = 5948, chance = 50}}, putInCorpse = true} -- tiger
}

	--- [itemEx.type] = {trashChance = , newId = ,trashId = , desc = , name = (only in non counted items)} / if trashId = 0 or nil, then remove


local materials_table = {

	[5878] = { -- leather_crafting
		--- leather bag
		[1] = {trashChance = 0, newId = 7343, desc = true, effect = CONST_ME_BLOCKHIT}, 
		--- leather boots
		[2] = {trashChance = 0, newId = 2643, trashId = 0, effect = CONST_ME_BLOCKHIT}, 
		--- leather legs
		[3] = {trashChance = 0, newId = 2649, trashId = 0, effect = CONST_ME_BLOCKHIT}, 
		--- rope
		[4] = {trashChance = 0, newId = 2120, trashId = 0, effect = CONST_ME_BLOCKHIT}, 
		--- jacket
		[5] = {trashChance = 0, newId = 2650, trashId = 0, effect = CONST_ME_BLOCKHIT}, 
		--- backpack
		[8] = {trashChance = 0, newId = 1988, trashId = 0, effect = CONST_ME_BLOCKHIT}, 
		--- leather armor
		[9] = {trashChance = 0, newId = 2467, trashId = 0, effect = CONST_ME_BLOCKHIT} 	
	},

	[5948] = { -- tiger_leather_crafting
		[8] = {trashChance = 0, newId = 2000, trashId = 0, effect = CONST_ME_BLOCKHIT} --- red backpack // 10522
		--- [8] = {chance = 100, newId = 2365, trashId = 0}   --- bp of holding
	},

	[5877] = { -- crocodile_leather_crafting
		[4] = {trashChance = 20, newId = 3982, trashId = 0, effect = CONST_ME_BLOCKHIT} --- crocodile boots
	},
	
	[5909] = { -- white piece of cloth
		[15] = {trashChance = 5, newId = 7183, trashId = 0, effect = CONST_ME_BLOCKHIT} --- baby seal doll
	},

	[11236] = { -- wool_crafting
		[3] = {trashChance = 5, newId = 5909, trashId = 0, effect = CONST_ME_BLOCKHIT}   --- white piece of cloth
	},
	
	[5901] = { -- wood
		[1] = {trashChance = 5, newId = 7426, effect = CONST_ME_BLOCKHIT} --- stick
	}

}
	
--[[
local 	recipe = {
	{newId = 11235, desc = true, components = {[1988] = 1, [5878] = 10}} --- expedition backpack = bp + 10 leather
}
]]

function onUse(cid, item, frompos, item2, topos)

	if transformTable[item2.itemid] ~= nil then  --- make letter
		doTransformItem(item2.uid, transformTable[item2.itemid])
		return true
	end

--	local percent, tempTable, removeTable = math.random(1, 100), {}, {}
	local CRAFTABLE, SKIN = nil, nil
	local specDescr = "Item maked by "..getCreatureName(cid).."."
	
	CRAFTABLE = CRAFTABLE ~= nil and CRAFTABLE or (materials_table[item2.itemid] ~= nil) and (materials_table[item2.itemid])[item2.type] or nil

	if CRAFTABLE ~= nil then
		crafting(cid, item, frompos, item2, topos, CRAFTABLE)
	end
	
	SKIN = skinable_corpse_table[item2.itemid] or nil
	
	if SKIN ~= nil then
		for _, item_properties_ in pairs(SKIN.item) do
			local count = (item_properties_.count ~= nil and item_properties_.count or SKIN.count ~= nil and SKIN.count or 1)
			count = type(count) == "table" and math.random(count[1], count[2]) or count
			local putInCorpse = (item_properties_.putInCorpse ~= nil and item_properties_.putInCorpse or SKIN.putInCorpse ~= nil and SKIN.putInCorpse or false)
			
			
			if (item_properties_.chance ~= nil and item_properties_.chance or SKIN.chance ~= nil and SKIN.chance or 100) >= math.random(1, 100) then
				if putInCorpse == true or putInCorpse == 1 then
					doAddContainerItem(item2.uid, item_properties_.id, count)
				else
					doPlayerAddItem(cid, item_properties_.id, count, true)
				end
			end
		end
		
		
		if SKIN.newitemEx_id == nil then
			doTransformItem(item2.uid, item2.itemid + 1)
			doDecayItem(item2.uid)
		elseif SKIN.newitemEx_id == 0 then
			doRemoveItem(item2.uid)
		else
			doTransformItem(item2.uid, SKIN.newitemEx_id)
			if SKIN.decay == true or SKIN.decay == 1 then
				doDecayItem(item2.uid)
			end
		end
		doSendMagicEffect(topos, CONST_ME_POFF)
		return true
	end
	return false
end